package core.util;

/**
 * @author songduk park
 * 
 */
public class Page {

	public static final String ROW_SIZE = "page_row_size";
	public static final String PAGE_REQUESTED = "page_requested";

	public int pageRequested;

	public final int totalPages;
	public final int totalRows;

	public final int startingRow;
	public final int endingRow;

	public final int startingPage;
	public final int endingPage;

	public Page(int totalRows, int pageSize, int pageRequested) {
		this.pageRequested = pageRequested;
		this.totalRows = totalRows;
		this.totalPages = (totalRows - 1) / pageSize + 1;

		if (pageRequested == 0)
			++pageRequested;

		if (pageRequested > totalPages)
			pageRequested = totalPages;

		//
		this.startingRow = (pageRequested - 1) * pageSize;

		if ((startingRow + pageSize) > totalRows)
			this.endingRow = totalRows;
		else
			this.endingRow = startingRow + pageSize;

		//
		if ((pageRequested - 20) < 1)
			this.startingPage = 1;
		else
			this.startingPage = pageRequested - 20;

		if ((startingPage + 40) > totalPages)
			this.endingPage = totalPages;
		else
			this.endingPage = startingPage + 40;

	}

	public String toString() {
		return "totalPages=" + totalPages + ", startingRow=" + startingRow
				+ ",endingRow=" + endingRow;
	}
}
